﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Code;
using Native.Tool.IniConfig;
using Native.Tool.IniConfig.Linq;

namespace PCRRPG.Code.EconomySystem
{
    public class EconomyOperater
    {
        static string GetMemberDataPath(long group, long qq)
        {
            var path = Path.Combine(AppMain.Common.AppDirectory, "saves", group.ToString(), qq.ToString() + "-eco.ini");
            var dir = Path.GetDirectoryName(path);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            if (!File.Exists(path))
                File.Create(path).Close();
            return path;
        }
        /// <summary>
        /// 为一个群员添加一定数量的货币
        /// </summary>
        /// <param name="Name">货币名称</param>
        /// <param name="count">数量</param>
        public static void AddMoney(long qq, long group, string name, int count)
        {

            var va = int.Parse( GetValue(qq, group, "货币", name, 0)) + count;
            WriteValue(qq, group, "货币", name, va.ToString());

        }

        /// <summary>
        /// 尝试扣除一定的货币
        /// </summary>
        /// <param name="qq"></param>
        /// <param name="group"></param>
        /// <param name="name"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static bool TryDeductMoney(long qq, long group, string name, int count)
        {
            var money = GetMoney(qq, group, name);
            if (money - count >= 0)
            {
                //操作成功
                var va = int.Parse(GetValue(qq, group, "货币", name, 0)) - count;
                WriteValue(qq, group, "货币", name, va);
                return true;
            }
            else
            {
                return false;
                //货币数量不够
            }
        }
        /// <summary>
        /// 获取货币数量
        /// </summary>
        /// <param name="qq"></param>
        /// <param name="group"></param>
        /// <param name="Name">货币名称</param>
        /// <returns></returns>
        public static int GetMoney(long qq, long group, string name)
        {
            return int.Parse(
            GetValue(qq, group, "货币", name, 0));
        }


        public static Dictionary<string, int> getAllMoney(long qq, long group)
        {

            Dictionary<string, int> dic = new Dictionary<string, int>();
            //GetValue(qq, group, "货币", "", 0);
            try
            {
                IniConfig ini = new IniConfig(GetMemberDataPath(group, qq));
                ini.Load();
                var section = "货币";
                if (!ini.Object.ContainsKey(section))
                    ini.Object.Add(new ISection(section));
                foreach (var item in ini.Object["货币"])
                {
                    dic.Add(item.Key, item.Value);
                }
            }
            catch (Exception)
            {

                throw;
            }
            return dic;
        }

        public static string GetValue(long qq,long group, string section, string key, object def)
        {
            IniConfig ini = new IniConfig(GetMemberDataPath(group, qq));
            ini.Load();
            if (!ini.Object.ContainsKey(section))
                ini.Object.Add(new ISection(section));
            if (!ini.Object[section].ContainsKey(key)) { 
                ini.Object[section].Add(key, new IValue(def));
                ini.Save();
            }
            return ini.Object[section][key].GetValueOrDefault(def).ToString();
        }
        public static bool WriteValue(long qq, long group, string section, string key, object value)
        {
            try
            {
                IniConfig ini = new IniConfig(GetMemberDataPath(group, qq));
                ini.Load();
                if (!ini.Object.ContainsKey(section))
                    ini.Object.Add(new ISection(section));
                if (!ini.Object[section].ContainsKey(key))
                    ini.Object[section].Add(key, new IValue(value));
                ini.Object[section][key] = new IValue(value);
                ini.Save();
                return true;
            }
            catch(Exception e)
            {
                //CQSave.CQLog.Info("写配置", $"发送错误，错误信息:{e.Message}");
                return false;
            }
        }

    }
}
